package kr.ac.mju.mdc.mjuapp;

import kr.ac.mju.mdc.mjuapp.util.PixelConverter;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

/**
 * @author davidkim
 *
 */
public class MainBtnPagerAdapter extends PagerAdapter {
	private final int CHILD_LAYOUT_COUNT = 2;

	private Context mContext;

	public MainBtnPagerAdapter(Context context) {
		// TODO Auto-generated constructor stub
		this.mContext = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return CHILD_LAYOUT_COUNT;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0 == arg1;
	}

	@Override
	public void destroyItem(ViewGroup pager, int position, Object view) {
		// TODO Auto-generated method stub
		((ViewPager) pager).removeView((View) view);
	}

	@Override
	public Object instantiateItem(ViewGroup pager, int position) {
		// TODO Auto-generated method stub
		//
		View view = null;
		// layout inflater
		LayoutInflater inflater = LayoutInflater.from(mContext);
		// set view
		if (position == 0) {
			view = inflater.inflate(R.layout.main_first_btn_layout, null);
			initFirstLayout(view);
		} else if (position == 1) {
			view = inflater.inflate(R.layout.main_second_btn_layout, null);
			initSecondLayout(view);
		}
		pager.addView(view);
		return view;
	}

	/**
	 * 
	 */
	private void initFirstLayout(View view) {
		// pixelConverter
		PixelConverter converter = new PixelConverter(mContext);
		// layoutparams
		LinearLayout.LayoutParams params = null;
		// main layout
		view.setPadding(converter.getWidth(19), 0, converter.getWidth(19), 0);
		// first row layout
		params = (LayoutParams) view.findViewById(R.id.main_first_btn_layout_first_row_layout).getLayoutParams();
		params.bottomMargin = converter.getHeight(8);
		view.findViewById(R.id.main_first_btn_layout_first_row_layout).setLayoutParams(params);
		// btn myiweb
		params = (LayoutParams) view.findViewById(R.id.main_btn_myiweb).getLayoutParams();
		params.width = converter.getWidth(292);
		params.height = converter.getHeight(135);
		params.rightMargin = converter.getWidth(8);
		view.findViewById(R.id.main_btn_myiweb).setLayoutParams(params);
		// btn introduce
		params = (LayoutParams) view.findViewById(R.id.main_btn_introduce).getLayoutParams();
		params.width = converter.getWidth(142);
		params.height = converter.getHeight(135);
		view.findViewById(R.id.main_btn_introduce).setLayoutParams(params);

		// second row layout
		params = (LayoutParams) view.findViewById(R.id.main_first_btn_layout_second_row_layout).getLayoutParams();
		params.bottomMargin = converter.getHeight(8);
		view.findViewById(R.id.main_first_btn_layout_second_row_layout).setLayoutParams(params);
		// btn library
		params = (LayoutParams) view.findViewById(R.id.main_btn_library).getLayoutParams();
		params.width = converter.getWidth(292);
		params.height = converter.getHeight(135);
		params.rightMargin = converter.getWidth(8);
		view.findViewById(R.id.main_btn_library).setLayoutParams(params);
		// btn notice
		params = (LayoutParams) view.findViewById(R.id.main_btn_notice).getLayoutParams();
		params.width = converter.getWidth(142);
		params.height = converter.getHeight(135);
		view.findViewById(R.id.main_btn_notice).setLayoutParams(params);

		// third row layout
		// btn complaint
		params = (LayoutParams) view.findViewById(R.id.main_btn_complaint).getLayoutParams();
		params.width = converter.getWidth(142);
		params.height = converter.getHeight(135);
		params.rightMargin = converter.getWidth(8);
		view.findViewById(R.id.main_btn_complaint).setLayoutParams(params);
		// btn food
		params = (LayoutParams) view.findViewById(R.id.main_btn_food).getLayoutParams();
		params.width = converter.getWidth(142);
		params.height = converter.getHeight(135);
		params.rightMargin = converter.getWidth(8);
		view.findViewById(R.id.main_btn_food).setLayoutParams(params);
		// btn campusmap
		params = (LayoutParams) view.findViewById(R.id.main_btn_admission).getLayoutParams();
		params.width = converter.getWidth(142);
		params.height = converter.getHeight(135);
		view.findViewById(R.id.main_btn_admission).setLayoutParams(params);
	}

	/**
	 * 
	 */
	private void initSecondLayout(View view) {
		// pixelConverter
		PixelConverter converter = new PixelConverter(mContext);
		// layoutparams
		LinearLayout.LayoutParams params = null;
		// main layout
		view.setPadding(converter.getWidth(19), 0, converter.getWidth(19), 0);
		// first row layout
		params = (LayoutParams) view.findViewById(R.id.main_second_btn_layout_first_row_layout).getLayoutParams();
		params.bottomMargin = converter.getHeight(8);
		view.findViewById(R.id.main_second_btn_layout_first_row_layout).setLayoutParams(params);
		// btn complaint01
		params = (LayoutParams) view.findViewById(R.id.main_btn_campusmap).getLayoutParams();
		params.width = converter.getWidth(142);
		params.height = converter.getHeight(135);
		params.rightMargin = converter.getWidth(8);
		view.findViewById(R.id.main_btn_campusmap).setLayoutParams(params);
		// btn food01
		params = (LayoutParams) view.findViewById(R.id.main_btn_community).getLayoutParams();
		params.width = converter.getWidth(142);
		params.height = converter.getHeight(135);
		params.rightMargin = converter.getWidth(8);
		view.findViewById(R.id.main_btn_community).setLayoutParams(params);
		// btn campusmap01
		params = (LayoutParams) view.findViewById(R.id.main_btn_phoneinfo).getLayoutParams();
		params.width = converter.getWidth(142);
		params.height = converter.getHeight(135);
		view.findViewById(R.id.main_btn_phoneinfo).setLayoutParams(params);
		// second row layout
		params = (LayoutParams) view.findViewById(R.id.main_second_btn_layout_second_row_layout).getLayoutParams();
		params.bottomMargin = converter.getHeight(8);
		view.findViewById(R.id.main_second_btn_layout_second_row_layout).setLayoutParams(params);
		// btn traffic
		params = (LayoutParams) view.findViewById(R.id.main_btn_traffic).getLayoutParams();
		params.width = converter.getWidth(142);
		params.height = converter.getHeight(135);
		view.findViewById(R.id.main_btn_traffic).setLayoutParams(params);
	}
}
/* end of file */